package com.sxau.software_educational_system_backend.controller;

import com.sxau.software_educational_system_backend.annotation.AdminOneOrTwo;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyOne;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyTwo;
import com.sxau.software_educational_system_backend.common.BaseResponse;
import com.sxau.software_educational_system_backend.common.ErrorCode;
import com.sxau.software_educational_system_backend.common.ResultUtils;
import com.sxau.software_educational_system_backend.domain.entity.Admin;
import com.sxau.software_educational_system_backend.domain.req.ReqAdminDTO;
import com.sxau.software_educational_system_backend.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sxau.software_educational_system_backend.constant.StatusConstant.DISABLE;

@RestController
@RequestMapping("/admins")
@RequiredArgsConstructor
@Api(tags = "管理员信息管理接口")
public class AdminController {

    private final AdminService adminService;

    @PostMapping
    @ApiOperation("新增管理员")
    @AdminOnlyTwo
    public BaseResponse<Integer> create(@RequestBody ReqAdminDTO reqAdminDTO) {
        int adminId = adminService.saveUser(reqAdminDTO);
        return ResultUtils.success(adminId);
    }

    @PutMapping("/{id}")
    @ApiOperation("禁用/启用管理员权限")
    @AdminOnlyTwo
    public BaseResponse<Void> toggleDisable(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        if (admin != null) {
            // 切换 isDelete 字段的值：0 变为 1，1 变为 0
            admin.setIsDelete(admin.getIsDelete() == 0 ? 1 : 0);
            adminService.updateById(admin);
        }
        return ResultUtils.success();
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除管理员")
    @AdminOnlyTwo
    public BaseResponse<Void> delete(@PathVariable Integer id) {
        adminService.removeById(id);
        return ResultUtils.success();
    }

    @GetMapping
    @ApiOperation("获取所有管理员信息")
    @AdminOneOrTwo
    public BaseResponse<List<Admin>> getAll() {
        List<Admin> admins = adminService.list();
        return ResultUtils.success(admins);
    }
}